﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Raise3000.Code.Foundation;

namespace Raise3000.Code.Attribute_Calculator
{
    public class CalculatorManager
    {
        private List<ICalculator> calculators_ = null;
        
        private List<string> listOfAllPlayersRegisteredToCalculators_ = null;

        private HandContext handContext_ = null;

        public CalculatorManager()
        {
            calculators_ = new List<ICalculator>();
            listOfAllPlayersRegisteredToCalculators_ = new List<string>();            
        }

        public void SetHandContext(HandContext hc)
        {
            handContext_ = hc;
        }

        public void RegisterCalculator(ICalculator calculator)
        {
            calculators_.Add(calculator);
        }

        public List<ICalculator> GetCalculators()
        {
            return calculators_;
        }

        public void OnStartHand()
        {
            // first, register player templates
            List<string> playerTemplates = handContext_.GetCurrentPlayers();
            foreach (string playerTemplate in playerTemplates)
            {
                RegisterPlayerToCalculators(playerTemplate);

                if (!listOfAllPlayersRegisteredToCalculators_.Contains(playerTemplate))
                {
                    listOfAllPlayersRegisteredToCalculators_.Add(playerTemplate);
                }
            }
            
            // and now dispatch the hand is started.
            HandContextHelper handContextHelper = new HandContextHelper(handContext_);
            foreach (ICalculator calculator in calculators_)
            {
                calculator.OnStartHand(handContextHelper);
            }
        }

        public void OnEndHand()
        {
            HandContextHelper handContextHelper = new HandContextHelper(handContext_);
            foreach (ICalculator calculator in calculators_)
            {
                calculator.OnEndHand(handContextHelper);
            }
        }

        public void StartPreflopActions()
        {
            handContext_.Street = StreetEnum.ePREFLOP;
        }

        public void EndPreflopActions()
        {
        }

        public void StartFlopActions()
        {
            handContext_.Street = StreetEnum.eFLOP;
        }

        public void EndFlopActions()
        {
        }

        public void StartTurnActions()
        {
            handContext_.Street = StreetEnum.eTURN;
        }

        public void EndTurnActions()
        {
        }

        public void StartRiverActions()
        {
            handContext_.Street = StreetEnum.eRIVER;
        }

        public void EndRiverActions()
        {
        }

        public void SetFlop(Card[] flop)
        {
            handContext_.SetFlop(flop);
        }

        public void SetTurn(Card turn)
        {
            handContext_.SetTurn(turn);
        }

        public void SetRiver(Card river)
        {
            handContext_.SetRiver(river);
        }

        public void DispatchAction(PlayerAction playerAction)
        {
            HandContextHelper handContextHelper = new HandContextHelper(handContext_);
            foreach (ICalculator calculator in calculators_)
            {
                calculator.DispatchAction(playerAction, handContextHelper);
            }

            // after dispatching, need to maintain the current hand context.
            handContextHelper.UpdateHandContext(playerAction);
        }

        public void RegisterPlayerToCalculators(string playerName)
        {
            foreach (ICalculator calculator in calculators_)
            {
                calculator.RegisterPlayer(playerName);
            }
        }

        public List<string> GetAllPlayersRegisteredToCalculators()
        {
            return listOfAllPlayersRegisteredToCalculators_;
        }
    }
}
